#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Author : zaniu(zzaniu@126.com)
# @Time   : 2021/6/23 14:51 
# @Desc   :
# 
#                   _ooOoo_
#                  o8888888o
#                  88" . "88
#                  (| -_- |)
#                  O\  =  /O
#               ____/`---'\____
#             .'  \\|     |//  `.
#            /  \\|||  :  |||//  \
#           /  _||||| -:- |||||-  \
#           |   | \\\  -  /// |   |
#           | \_|  ''\---/''  |   |
#           \  .-\__  `-`  ___/-. /
#         ___`. .'  /--.--\  `. . __
#      ."" '<  `.___\_<|>_/___.'  >'"".
#     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
#     \  \ `-.   \_ __\ /__ _/   .-` /  /
# =====`-.____`-.___\_____/___.-`____.-'=====
#                   `=---='
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#             佛祖保佑       永无BUG
#
import grpc
from grpc._channel import _InactiveRpcError

from learn_grpc_001.proto import hello_pb2_grpc, hello_pb2

channel = grpc.insecure_channel('localhost:50051')


def hello():
    stub = hello_pb2_grpc.GreeterStub(channel)
    response = stub.SayHello(hello_pb2.HelloRequest(name='沙雕'))
    print("Greeter client received: " + response.message)


def addInt(value1: int, value2: int):
    stub = hello_pb2_grpc.AddServerStub(channel)
    try:
        response = stub.AddInt(hello_pb2.AddIntRequest(value1=value1, value2=value2))
    except _InactiveRpcError as e:
        print(f"e.code() = {e.code()}")
        print(f"e.details() = {e.details()}")
        return 0
    print("Greeter client received: ", response.message)
    return response.message


def addFloat(value1: float, value2: float):
    # 获取存根，其实就是指定channel发送固定字符串
    stub = hello_pb2_grpc.AddServerStub(channel)
    # 用存根调用远程方法
    res = stub.AddFloat(hello_pb2.AddFloatRequest(value1=value1, value2=value2))
    return res.message


if __name__ == '__main__':
    hello()
    print(addInt(1, 4))
    print(addFloat(1, 4))
